<template>
  <div class="machineDeployTemplate-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="管路序号（从1开始排序）" prop="pipelineSeq">
          <el-input
            placeholder="请输入管路序号（从1开始排序）"
            v-model="searchForm.pipelineSeq"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="料仓名称" prop="siloName">
          <el-input
            placeholder="请输入料仓名称"
            v-model="searchForm.siloName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="绑定物料ID（关联material表）" prop="materialId">
          <el-input
            placeholder="请输入绑定物料ID（关联material表）"
            v-model="searchForm.materialId"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="是否无限量(0-否 1-是)" prop="isUnlimited">
          <el-input
            placeholder="请输入是否无限量(0-否 1-是)"
            v-model="searchForm.isUnlimited"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="料仓容量（单位：千克，无限量时填NULL）"
          prop="capacity"
        >
          <el-input
            placeholder="请输入料仓容量（单位：千克，无限量时填NULL）"
            v-model="searchForm.capacity"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="料仓分组（A/B/C类分组）" prop="siloGroup">
          <el-input
            placeholder="请输入料仓分组（A/B/C类分组）"
            v-model="searchForm.siloGroup"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="清洗分组（空值表示不分组）" prop="cleaningGroup">
          <el-input
            placeholder="请输入清洗分组（空值表示不分组）"
            v-model="searchForm.cleaningGroup"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input
            placeholder="请输入创建人"
            v-model="searchForm.createdBy"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="更新人" prop="updatedBy">
          <el-input
            placeholder="请输入更新人"
            v-model="searchForm.updatedBy"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="" prop="createdAt">
          <el-input
            placeholder="请输入"
            v-model="searchForm.createdAt"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="" prop="updatedAt">
          <el-input
            placeholder="请输入"
            v-model="searchForm.updatedAt"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增机台部署模板表
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="id" label="主键ID" width="190" />
          <el-table-column
            prop="pipelineSeq"
            label="管路序号（从1开始排序）"
            width="190"
          />
          <el-table-column prop="siloName" label="料仓名称" width="190" />
          <el-table-column
            prop="materialId"
            label="绑定物料ID（关联material表）"
            width="190"
          />
          <el-table-column
            prop="isUnlimited"
            label="是否无限量(0-否 1-是)"
            width="190"
          />
          <el-table-column
            prop="capacity"
            label="料仓容量（单位：千克，无限量时填NULL）"
            width="190"
          />
          <el-table-column
            prop="siloGroup"
            label="料仓分组（A/B/C类分组）"
            width="190"
          />
          <el-table-column
            prop="cleaningGroup"
            label="清洗分组（空值表示不分组）"
            width="190"
          />
          <el-table-column prop="createdBy" label="创建人" width="190" />
          <el-table-column prop="updatedBy" label="更新人" width="190" />
          <el-table-column prop="createdAt" label="" width="190" />
          <el-table-column prop="updatedAt" label="" width="190" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <MachineDeployTemplateAdd v-model="addUpdateRef"></MachineDeployTemplateAdd>
    <MachineDeployTemplateRemove
      v-model="removeRef"
    ></MachineDeployTemplateRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getMachineDeployTemplatePagination,
  getMachineDeployTemplateAllList,
} from '@/api/dms-machine-deploy-template.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import MachineDeployTemplateRemove from './MachineDeployTemplateRemove.vue'
import MachineDeployTemplateAdd from './MachineDeployTemplateAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `机台部署模板表`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `机台部署模板表`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      pipelineSeq: '',
      siloName: '',
      materialId: '',
      isUnlimited: '',
      capacity: '',
      siloGroup: '',
      cleaningGroup: '',
      createdBy: '',
      updatedBy: '',
      createdAt: '',
      updatedAt: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    pipelineSeq: item.pipelineSeq,
    siloName: item.siloName,
    materialId: item.materialId,
    isUnlimited: item.isUnlimited,
    capacity: item.capacity,
    siloGroup: item.siloGroup,
    cleaningGroup: item.cleaningGroup,
    createdBy: item.createdBy,
    updatedBy: item.updatedBy,
    createdAt: item.createdAt,
    updatedAt: item.updatedAt,
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `机台部署模板表`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  pipelineSeq: '',
  siloName: '',
  materialId: '',
  isUnlimited: '',
  capacity: '',
  siloGroup: '',
  cleaningGroup: '',
  createdBy: '',
  updatedBy: '',
  createdAt: '',
  updatedAt: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let pipelineSeq = searchForm.value.pipelineSeq
  if (pipelineSeq) {
    searchCondition.pipelineSeq = pipelineSeq
  }
  let siloName = searchForm.value.siloName
  if (siloName) {
    searchCondition.siloName = siloName
  }
  let materialId = searchForm.value.materialId
  if (materialId) {
    searchCondition.materialId = materialId
  }
  let isUnlimited = searchForm.value.isUnlimited
  if (isUnlimited) {
    searchCondition.isUnlimited = isUnlimited
  }
  let capacity = searchForm.value.capacity
  if (capacity) {
    searchCondition.capacity = capacity
  }
  let siloGroup = searchForm.value.siloGroup
  if (siloGroup) {
    searchCondition.siloGroup = siloGroup
  }
  let cleaningGroup = searchForm.value.cleaningGroup
  if (cleaningGroup) {
    searchCondition.cleaningGroup = cleaningGroup
  }
  let createdBy = searchForm.value.createdBy
  if (createdBy) {
    searchCondition.createdBy = createdBy
  }
  let updatedBy = searchForm.value.updatedBy
  if (updatedBy) {
    searchCondition.updatedBy = updatedBy
  }
  let createdAt = searchForm.value.createdAt
  if (createdAt) {
    searchCondition.createdAt = createdAt
  }
  let updatedAt = searchForm.value.updatedAt
  if (updatedAt) {
    searchCondition.updatedAt = updatedAt
  }

  tableLoading.value = true
  getMachineDeployTemplatePagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.machineDeployTemplate-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.machineDeployTemplate-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
